package com.cet.algorithm.哈希表;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: algorithm
 * @description: 四数和
 * @author: 陈恩涛
 * @create: 2022-03-16 10:28
 **/
public class LC454 {

    public static int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map<Integer, Integer> map = new HashMap<>();
        int result = 0;
        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < nums2.length; j++) {
                int temp = nums1[i] + nums2[j];
                map.merge(temp, 1, Integer::sum);
            }
        }
        for (int i = 0; i < nums3.length; i++) {
            for (int j = 0; j < nums4.length; j++) {
                if (map.containsKey(-(nums3[i] + nums4[j]))) {
                    result += map.get(-(nums3[i] + nums4[j]));
                }
            }
        }
        return result;
    }
}
